<template>
  <div class="mainsu">
    <div class="main_spac1">
      <div class="main_white padding10 paddingBottom20">
        <div class="mainor_form">
          <div class="mainor_te">
            <el-button @click="export_detail" class="mainor_tea"
              >导出明细</el-button
            >
            <el-button @click="export_all">导出汇总</el-button>
          </div>
          <el-form
            :inline="true"
            :model="form"
            class="demo-form-inline"
            ref="userListFrom"
            label-width="70px"
          >
            <el-form-item label="配送日期" prop="send_time">
              <el-date-picker
                v-model="form.send_time"
                type="date"
                class="wid220"
                :editable="false"
                placeholder="选择配送日期"
                value-format="yyyy-MM-dd"
                :clearable="false"
              />
            </el-form-item>
            <el-form-item label="分类" prop="category_id" class="wid220">
              <el-cascader
                v-model="form.category_id"
                :options="options"
                :props="defaultProps"
              ></el-cascader>
            </el-form-item>
            <div class="more el-form-item" @click="more">
              <span>更多</span>
              <i :class="[{ on: moredata }, ' el-icon-arrow-down']"></i>
            </div>
            <div class="clearfix"></div>
            <el-collapse-transition>
              <div v-if="moredata">
                <el-form-item label="商品名称" prop="name">
                  <el-input
                    v-model.trim="form.name"
                    placeholder="商品名称"
                    clearable
                    class="wid220"
                  ></el-input>
                </el-form-item>
                <div class="clearfix"></div>
              </div>
            </el-collapse-transition>
            <el-form-item class="marginTop10">
              <el-button
                class="reset marginLeft10"
                type="primary"
                @click="$refs['userListFrom'].resetFields()"
                >重置</el-button
              >
              <el-button
                type="primary"
                @click="getArticleList"
                :loading="listLoading"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </div>
        <div class="main_table marginTop10" style="min-height: 750px">
          <el-table
            ref="multipleTable"
            v-loading="listLoading"
            :data="list.list"
            element-loading-text="加载中"
            fit
            border
            highlight-current-row
            :row-class-name="getRowClass"
            @select="onTableSelect"
            @expand-change="expandChange"
          >
            <el-table-column type="expand" class-name="main_hei">
              <template slot-scope="props">
                <el-table
                  :ref="'expandchild' + props.$index"
                  :data="props.row.order_list"
                  class="mainex_ta"
                  fit
                  :row-class-name="getRowClass"
                  :highlight-current-row="false"
                  @select="
                    (rows, row) => {
                      expandSelect(rows, row, props.$index);
                    }
                  "
                >
                  <!-- 表格前的选中框 -->
                  <!-- <el-table-column type="selection" width="95" align="right" /> -->
                  <el-table-column label="订单号" prop="order_sn" width="200" />
                  <el-table-column label="商户名称" prop="nickname" />
                  <el-table-column label="下单数量" prop="order_num" />
                  <el-table-column label="下单单价" prop="order_price" />
                  <el-table-column label="下单金额" prop="order_money" />
                  <el-table-column label="配送数量" prop="send_num" />
                  <el-table-column label="配送单价" prop="sort_price" />
                  <el-table-column label="配送金额" prop="send_money" />
                  <el-table-column label="备注" prop="remark" />
                </el-table>
              </template>
            </el-table-column>

            <!-- <el-table-column type="selection" /> -->
            <el-table-column label="序号" width="100">
              <template slot-scope="scope">{{
                (current.page - 1) * current.per_page + scope.$index + 1
              }}</template>
            </el-table-column>
            <el-table-column label="商品名称" width="200" prop="name" />
            <el-table-column label="单位" prop="unit" />
            <el-table-column label="下单总量" width="120" prop="order_num" />
            <el-table-column label="下单总额" width="120" prop="order_money" />
            <el-table-column label="配送总量" width="120" prop="send_num" />
            <el-table-column label="配送总额" width="120" prop="send_money" />
          </el-table>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {
  orderList_index,
  orderSummary_export_detail,
  orderSummary_export_all,
} from "@/api/order/orderSummary";
import { apiUrl, objKeySort, Md5 } from "@/utils/public";
import { recursion, category, parseTime } from "@/utils/index";

export default {
  components: {},
  data() {
    return {
      // 时间
      parseTime,
      // 分页
      current: {
        page: 1,
        per_page: 20,
      },
      limit: 20,
      totalCount: 1,
      // 列表数据
      list: {
        list: [],
      },
      listLoading: false,
      // 搜索
      form: {
        name: "",
        pid: "",
        category_id: [],
        send_time: parseTime(new Date(), "{y}-{m}-{d}"),
      },
      // 更多
      moredata: false,
      // 分类
      options: [],
      defaultProps: {
        children: "selfNext",
        label: "title",
        value: "id",
        checkStrictly: true,
      },
    };
  },
  created() {
    // 列表
    this.orderList_index();
  },
  methods: {
    // 导出明细
    export_detail() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form()
      );
      orderSummary_export_detail(data)
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 导出汇总
    export_all() {
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(Object.assign(apiUrl().name, this.list_form())) +
              apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form()
      );
      orderSummary_export_all(data)
        .then((res) => {
          const link = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
        })
        .catch(() => {
          this.$message.error("导出失败");
        });
    },
    // 判断表格是否有子项，无子项不显示展开按钮
    getRowClass(row) {
      row.row.index = row.rowIndex;
      if (row.row.order_list) {
        if (row.row.order_list.length === 0) {
          return "row-expand-cover";
        }
      }
    },
    // 监听展开行
    expandChange(rows, row) {
      if (rows.checked) {
        rows.order_list.forEach((chil) => {
          setTimeout(() => {
            if (chil.checked) {
              if (this.$refs["expandchild" + rows.index]) {
                this.$refs["expandchild" + rows.index].toggleRowSelection(
                  chil,
                  true
                );
              }
            }
          });
        });
      }
    },
    // 监听一级是否勾选
    onTableSelect(rows, row) {
      row.checked = !row.checked;
      if (row.checked) {
        row.order_list.forEach((m) => {
          if (this.$refs["expandchild" + row.index]) {
            this.$refs["expandchild" + row.index].toggleRowSelection(m, true);
          }
          m.checked = true;
        });
      } else {
        row.order_list.forEach((i) => {
          if (this.$refs["expandchild" + row.index]) {
            this.$refs["expandchild" + row.index].clearSelection();
          }
          i.checked = false;
        });
      }
    },
    // 监听二级是否勾选
    expandSelect(rows, row, index) {
      row.checked = !row.checked;
      if (rows.length != 0) {
        this.list.list[index].checked = true;
        this.$refs.multipleTable.toggleRowSelection(
          this.list.list[index],
          true
        );
      } else {
        this.list.list[index].checked = false;
        this.$refs.multipleTable.toggleRowSelection(
          this.list.list[index],
          false
        );
      }
    },
    // 搜索
    getArticleList() {
      this.current.page = 1;
      this.orderList_index();
    },
    // 分页
    changeLimit(pageSize) {
      this.current.per_page = pageSize;
      this.orderList_index();
    },
    changePage(page) {
      this.current.page = page;
      this.orderList_index();
    },
    // 更多
    more() {
      this.moredata = !this.moredata;
    },
    // 列表
    orderList_index() {
      this.listLoading = true;
      let data = Object.assign(
        {
          sign: Md5(
            objKeySort(
              Object.assign(apiUrl().name, this.list_form(), this.current, {
                status: this.activeName,
              })
            ) + apiUrl().appsecret
          ),
        },
        apiUrl().name,
        this.list_form(),
        this.current,
        { status: this.activeName }
      );
      orderList_index(data).then((res) => {
        this.listLoading = false;
        if (res.errcode == 0) {
          this.options = recursion(res.data.categoryList, true);
          res.data.list.map((e) => {
            e.checked = false;
            e.order_list.map((p) => {
              p.checked = false;
            });
          });
          this.list = res.data;
          this.totalCount = res.data.list.total;
        } else {
          this.$message.error(res.errmsg);
        }
      });
    },
    list_form() {
      let form = JSON.parse(JSON.stringify(this.form));
      form.pid = form.category_id[0] ? form.category_id[0] : "";
      form.category_id = category(form.category_id);

      return form;
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/styles/variables.scss";

.mainsu {
  .mainor_form form {
    width: calc(100% - 180px);
  }

  .main_table {
    margin-top: 20px;
  }
}
</style>
